<?php
namespace App\Http\Controllers\Api\V1;

use App\Models\Product;
use App\Models\ProductImage;
use App\TraitHelper\RequestHelper;

/**
 * Created by PhpStorm.
 * User: xinghuo
 * Date: 2017/7/11
 * Time: 上午12:53
 */
class ProductController
{
    use RequestHelper;

    public function index()
    {
        $product =  Product::firstOrCreate(["id"=>1]);
        $banners = $product->images()->where('type',ProductImage::TYPE_BANNER)->get();
        $images = $product->images()->where('type',ProductImage::TYPE_PULL_IMAGE)->get();
        $imgs =[];
        if ($banners) {
            foreach ($banners as $v) {
                $imgs[] = isset($v->image) ? url($v->image):Product::$default_image;

            }
        }
        $data['product'] = [
            'title' => $product->title,
            'price' => $product->price,
            'number' => $product->number,
            'images' => $imgs,
        ];
        if ($images) {
            foreach ($images as $image) {
                if ($image->image) {
                    $data['images'][] =  url($image->image);
                }
            }
        }
        $data['shareTip'] = $product->shareTip;
        $data['shipTip'] = $product->shipTip;
        $data['discountTip'] = $product->discountTip;
        $data['des'] = $product->des;
        $data['notice'] = $product->notice;
        $data['install_price'] = Product::$install_price;
        $data['open_acount_price'] = Product::$open_acount_price;
        $data['service_price'] = Product::$service_price;
        $data['discount_price'] = Product::$discount_price;
        $data['shareTitle'] = '芯好水好';
        $data['shareContent'] = '芯好水好,0元购机你信吗?';
        $data['shareImage'] = 'http://water.phpyiqiwan.com/logo.png';
        $data['shareUrl'] =  route('url.go', ['url'=>'http://water.phpyiqiwan.com/#/']) ;
        $data['sellCount'] = 5;
        $data['service_call'] = '4001782899';

        return $this->formatJson(true,$data);

    }

    public function detail()
    {
        $product =  Product::firstOrCreate(["id"=>1]);

        $data['info'] = Product::formatInfo($product->info);

        $data['packageTip'] = $product->packageTip;
        $data['shouTip'] = $product->shouTip;
        $data['priceTip'] = $product->priceTip;
        return $this->formatJson(true,$data);
    }
}